package cn.rongdao.jrkabao.ui.remind.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.ui.adapter.recyclerview.FRBaseRecyclerAdapter;
import com.mifengkong.frtools.util.FRString;
import com.mifengkong.frtools.util.FRToast;
import com.mifengkong.frwidget.recyclerview.PtrRecyclerViewUIComponent;
import com.mifengkong.frwidget.recyclerview.divider.RecyclerViewDivider;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.base.BaseFragment;
import cn.rongdao.jrkabao.bean.remind.RemindMailListBean;
import cn.rongdao.jrkabao.model.remind.ServiceModel;
import cn.rongdao.jrkabao.ui.cashadvance.uicomponent.CashEmptyUIComponent;
import cn.rongdao.jrkabao.ui.remind.adapter.RemindMailListAdapter;
import cn.rongdao.jrkabao.ui.remind.adapter.RemindMailListViewHolder;
import cn.rongdao.jrkabao.util.CommonUtil;

/**
 * Created by jack on 2017/11/21
 */

public class FragmentService extends BaseFragment implements FRBaseRecyclerAdapter.OnItemClickListener<RemindMailListViewHolder> {

    @BindView(R.id.fs_frame_layout)
    PtrRecyclerViewUIComponent ptrRecyclerViewUIComponent;
    @BindView(R.id.fs_empty_view)
    CashEmptyUIComponent cashEmptyUIComponent;

    private RemindMailListAdapter mAdapter;
    private String remindId;

    @Override
    protected int layoutRes() {
        return R.layout.fragment_service;
    }

    @Override
    protected void onViewReallyCreated(View view) {
        super.onViewReallyCreated(view);
        initPtr();
    }

    private void initPtr() {
        mAdapter = new RemindMailListAdapter(getActivity(), this);
        ptrRecyclerViewUIComponent.setRecyclerViewDivider(new RecyclerViewDivider(getActivity()));
        ptrRecyclerViewUIComponent.setLayoutManager(new LinearLayoutManager(getActivity()));
        ptrRecyclerViewUIComponent.setAdapter(mAdapter);
        ptrRecyclerViewUIComponent.setCanRefresh(false);
        getServiceListData();
    }

    public void setRemindId(String remindId) {
        this.remindId = remindId;
    }

    private void getServiceListData() {
        if (FRString.isEmpty(remindId)) {
            List<RemindMailListBean> dataList = new ArrayList<>();
            RemindMailListBean remindMailListBean = new RemindMailListBean();
            remindMailListBean.setService_desc("可用额度查询");
            remindMailListBean.setService_tips("发送ZDCX 1234 到 95528");
            dataList.add(remindMailListBean);
            mAdapter.setDataList(dataList);
            return;
        }
        ServiceModel.getInstance().execute(remindId, new FRBaseObserver<List<RemindMailListBean>>(this) {
            @Override
            protected void onBaseNext(List<RemindMailListBean> data) {
                if (null != data && data.size() > 0) {
                    mAdapter.setDataList(data);
                    setPtrFrameLayoutVisible(true);
                } else {
                    setPtrFrameLayoutVisible(false);
                }
            }

            @Override
            protected void onBaseError(Throwable t) {
                super.onBaseError(t);
                setPtrFrameLayoutVisible(false);
            }
        });
    }

    private void setPtrFrameLayoutVisible(boolean isVisible) {
        if (isVisible) {
            cashEmptyUIComponent.setVisibility(View.GONE);
        } else {
            cashEmptyUIComponent.setRemindServiceEmpty();
        }
        ptrRecyclerViewUIComponent.setVisibility(isVisible ? View.VISIBLE : View.GONE);
        ptrRecyclerViewUIComponent.refreshComplete();
    }

    @Override
    public void onItemClick(RemindMailListViewHolder remindMailListViewHolder, int position) {
        if (FRString.isEmpty(remindId)) {
            FRToast.showToastSafe("不妨添加信用卡试试");
            return;
        }
        String phone = mAdapter.getDataList().get(position).getService_phone();
        String msg = mAdapter.getDataList().get(position).getService_content();
        startActivity(CommonUtil.getMsgIntent(phone, msg));
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}